﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;
using System.Text;

class Program
{
    public class FractionNumber : IFormattable
    {
        private int enumerator;
        private int denominator;

        public FractionNumber(int enumer, int denomin)
        {
            this.enumerator = enumer;
            this.denominator = denomin;
        }


        public override string ToString()
        {
            return string.Format("{0}", (double)enumerator / denominator);
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case "n":
                case "N":
                    return string.Format("{0}/{1}", this.enumerator, this.denominator);              
                case "r":
                case "R":
                    return string.Format("{0:f2}", (double)enumerator / denominator);
                case null:
                    return this.ToString();
                default:
                    //Console.WriteLine(new StackTrace());
                    return string.Format("Invalid Format: {0}", format);
            }
        }
    }

    static void Main(string[] args)
    {
        string sampleString = "Hi!";
        Console.WriteLine(@"\x{0:X}\x{1:X}\x{2:X}", Convert.ToInt32(sampleString[0]), 
                          Convert.ToInt32(sampleString[1]), Convert.ToInt32(sampleString[2]));
        Console.WriteLine();

        for (int i = 5; i < 26; i++)
        {
            Console.WriteLine("{0,5}|{1,-6}|{2:000000.0000}|{3}", i, i*i, Math.Sqrt(i), Math.Log(i));
        }
        Console.WriteLine();
        FractionNumber fn = new FractionNumber(1, 3);
        Console.WriteLine(fn);
        Console.WriteLine("{0:r}", fn);
        Console.WriteLine("{0:N}", fn);
        Console.WriteLine();

        //Console.Write("Enter date: ");
        //string date = Console.ReadLine();

        //DateTime dt = DateTime.Parse(date, new CultureInfo("bg-BG").DateTimeFormat);
        //Console.WriteLine("day={0} month={1} year={2}", dt.Day, dt.Month, dt.Year);
        //Console.WriteLine();

        //Console.Write("Enter a real number: ");
        //double number = Convert.ToDouble(Console.ReadLine());


        //StringBuilder sb = new StringBuilder(string.Format(new CultureInfo("de-AT"), "Amount: {0:C}, Culture: {1}\n", number, "Bulgaria"));
        //sb.Append(string.Format(new CultureInfo("bg-BG"), "Amount: {0:C}, Culture: {1}\n", number, "Bulgaria"));
        //sb.Append(string.Format(new CultureInfo("he-IL"), "Amount: {0:C}, Culture: {1}\n", number, "Israel"));
        //sb.Append(string.Format(new CultureInfo("mn-MN"), "Amount: {0:C}, Culture: {1}\n", number, "Mongolia"));
        //sb.Append(string.Format(new CultureInfo("zh-TW"), "Amount: {0:C}, Culture: {1}\n", number, "Taiwan"));
        //sb.Append(string.Format(new CultureInfo("ja-JP"), "Amount: {0:C2}, Culture: {1}\n", number, "Japan"));
        //MessageBox.Show(sb.ToString());

        //Console.Write("Enter a number: ");
        //string num = Console.ReadLine();
        //num = num.Replace(" ", "");
        //string newNum = num.Replace(",", ".");

        //double value = double.Parse(newNum, NumberStyles.Number, new CultureInfo("en-US").NumberFormat);
        //Console.WriteLine(value);

        string strForEncoding = "some string - @ ® ™";
        byte[] byteArray = Encoding.ASCII.GetBytes(strForEncoding);
        foreach (var item in byteArray)
        {
            Console.Write("0x{0:X} ", item);
        }
        Console.WriteLine("ASCII");
        byteArray = Encoding.Unicode.GetBytes(strForEncoding);
        foreach (var item in byteArray)
        {
            Console.Write("0x{0:X} ", item);
        }
        Console.WriteLine("Unicode");
        byteArray = Encoding.UTF8.GetBytes(strForEncoding);
        foreach (var item in byteArray)
        {
            Console.Write("0x{0:X} ", item);
        }
        Console.WriteLine("UTF8");
        Encoding win1251 = Encoding.GetEncoding("windows-1251");
        byteArray = win1251.GetBytes(strForEncoding);
        foreach (var item in byteArray)
        {
            Console.Write("0x{0:X} ", item);
        }
        Console.WriteLine("Windows-1251");
        EncodingInfo[] allEncoders =  Encoding.GetEncodings();
        foreach (var item in allEncoders)
        {
            byteArray = item.GetEncoding().GetBytes(strForEncoding);
            foreach (var b in byteArray)
            {
                Console.Write("0x{0:X} ", b);
            }
            Console.WriteLine();
        }
    }
}

